/*******************************************************************************
 * Copyright 2011 @ Kapil Viren Ahuja
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 ******************************************************************************/
package com.kapil.framework.lang;

import java.util.Calendar;

import org.apache.commons.lang.StringUtils;

import com.kapil.framework.exception.impl.ObjectInitializationException;
import com.kapil.framework.logger.ILogger;
import com.kapil.framework.logger.LogFactory;

/**
 * <p>
 * Utility class used to create objects of other classes.
 * </p>
 */
@SuppressWarnings("rawtypes")
public final class ObjectFactory
{


    private static final ILogger LOGGER = LogFactory.getInstance().getLogger(ObjectFactory.class);


    /**
     * <p>
     * Made private to prevent from instances getting created
     * </p>
     * .
     */
    private ObjectFactory()
    {
    }


    /**
     * <p>
     * Creates an instance of the class passed in the parameter.
     * </p>
     * 
     * @param className - a {@link String} containing name of the class for which the instance has to be created
     * 
     * @return initialized {@link Object}
     */
    public static Object createInstance(final String className)
    {
        Object clazz;

        try
        {
            clazz = Class.forName(className).newInstance();
            LOGGER.debug("Instace of class with the name [" + className + "] created successfully.");

            return clazz;
        }
        catch (InstantiationException iex)
        {
            throw new ObjectInitializationException(ObjectInitializationException.CLASS_NOT_FOUND, new String[] { className }, iex);
        }
        catch (IllegalAccessException iaex)
        {
            throw new ObjectInitializationException(ObjectInitializationException.ILLEGAL_ACCESS, new String[] { className }, iaex);
        }
        catch (ClassNotFoundException cnfex)
        {
            throw new ObjectInitializationException(ObjectInitializationException.CLASS_NOT_FOUND, new String[] { className }, cnfex);
        }
    }


    public static Object createInstance(final Class clazz) throws ObjectInitializationException
    {
        return createInstance(clazz.getCanonicalName());
    }


    public static Object createInstance(final String className, final String data) throws ObjectInitializationException
    {
        Object obj = null;

        if (StringUtils.equalsIgnoreCase(data, "null"))
        {
            obj = null;
        }
        else if (StringUtils.equalsIgnoreCase(className, "long"))
        {
            obj = new Long(data);
        }
        else if (StringUtils.equalsIgnoreCase(className, "string"))
        {
            obj = new String(data);
        }
        else if (StringUtils.equalsIgnoreCase(className, "integer"))
        {
            obj = new Integer(data);
        }
        else if (StringUtils.equalsIgnoreCase(className, "boolean"))
        {
            if (data.equals(Boolean.TRUE))
            {
                obj = Boolean.TRUE;
            }
            else
            {
                obj = Boolean.FALSE;
            }
        }
        else if (StringUtils.equalsIgnoreCase(className, "Date"))
        {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, Integer.parseInt(data.substring(0, data.indexOf("-"))));
            c.set(Calendar.MONTH, Integer.parseInt(data.substring(data.indexOf("-") + 1, data.lastIndexOf("-"))) - 1);
            c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(data.substring(data.lastIndexOf("-") + 1, data.length())));
            obj = c.getTime();
        }

        return obj;
    }
}
